
# Emacs Cheatsheet

As a keyboard focused editor, emacs makes text editing efficient with the use of "control characters" like `<RET>`, `<TAB>`, `<DEL>`, `<ESC>`, `<F1>`, `<Home>`, etc. Emacs extends this efficiency by recognizing control characters that are entered using "modifier keys", these keys are `<Ctrl>`, `<Meta>` or `<Alt>`. With these modifier keys you can type chords or "key-chords" which are combinations of key-strokes. Here a “key chord” means two keys pressed simultaneously, or a single key quickly pressed twice. These chords are bind to an emacs command to execute it faster than typing the full command name.

These chords are composed of a modifier key and simple characters. For example, `<Ctrl>-a` is entered by holding down the `<Control>` key while pressing `a`. When using emacs and reading its documentation you can see these chords are short as `C-a`, `M-x`, etc. Where C is short for `<Ctrl>`, `M` is short for `<Meta>` or `<Alt>`; so if you see `C-M-a` this means you press and hold down `<Ctrl>`, `<Meta>` together and then press `a`.

If you see a space inside a chord like this `C-x f`, this means you press and release the chord prefix `C-x` and then type `f`.


## Getting Help In Emacs

### Basic Help Commands

| Key/Chord | Action |
| --------- | ------ |
| `?` | Describe the mode you are currently in, if available. This will show a description, commands and key bindings available in the current major mode |
| `C-h ?` | Enter 'metahelp' mode with all available options to get help about everything inside emacs |


### Commands, Keys And Functions

| Key/Chord | Action |
| --------- | ------ |
| `C-h m` | Show help for the current major and minor modes |
| `C-h b` | Show all key bindings, that is a list of key bindings and associated commands or functions |
| `C-h k` | Show help for key, prompts you to enter the key or key chord and shows you the help for the command or function it calls and alternate key bindings |
| `C-h c` | Show a brief help for key, just a description of what it does |
| `C-h w` | Show which key runs a specific command, prompts you for a command. Press <TAB> for a list of commands |
| `C-h a` | Search for commands, runs 'apropos-command', equivalent of 'M-x apropos'. Prompts you for a regular expression, opens a new buffer with the matching commands |
| `C-h d` | Search documentation of functions, variables and other items |
| `C-h x` | Show help for command |
| `C-h f` | Show help for function |
| `C-h v` | Show help for variable |
| `C-h o` | Show help for function or variable |


### Manuals

| Key/Chord | Action |
| --------- | ------ |
| `C-h r` | Show emacs manual |
| `C-h F` | Show emacs manual section for command |
| `C-h K` | Show emacs manual for a key sequence |
| `C-h i` | Show all installed manuals |
| `C-h R` | Show a specific manual from all installed |
| `C-h S` | Show a description of a symbol in pertinent manual |


### Other Help Commands

| Key/Chord | Action |
| --------- | ------ |
| `C-h C-e` | Extending emacs with external packages |
| `C-h p` | Search for emacs packages in a Finder mode menu, you can also do a list with 'M-x list-packages' |
| `C-h P` | Describe a specific emacs package |
| `C-h t` | Start the emacs tutorial |
| `C-h e` | Show recent messsages from echo area |
| `C-h l` | Show the last 300 input keystrokes |
| `C-h .` | Show local help at point |


### Miscelaneous Help

| Key/Chord | Action |
| --------- | ------ |
| `C-h C-a` | Open About emacs buffer |
| `C-h C-f` | Open emacs FAQ info buffer |
| `C-h C-n` | Open emacs news of recent changes buffer |
| `C-h C-p` | Open emacs known problems buffer |
| `C-h C-d` | Open debugging emacs buffer |
| `C-h g` | Open the "about the GNU project" web page in browser |
| `C-h C-c` | Open the "copying permission" buffer with the General Public Licence (GPLv3+) |
| `C-h C-o` | Open the "ordering and distribution" information buffer |
| `C-h C-m` | Open the information buffer to order printed manuals |
| `C-h C-t` | Open the emacs project TODO list |
| `C-h C-w` | Open information buffer on absence of warranty |


### Internationalization and Coding Systems

| Key/Chord | Action |
| --------- | ------ |
| `C-h I` | Describe an input method, if you press <TAB> you can get a list of input methods |
| `C-h C` | Describe the system's coding system (utf-8, etc) |
| `C-h L` | Describe the system's language environment |
| `C-h s` | Show the current syntax table |
| `C-h h` | Display the HELLO file illustrating various scripts |


## General Key Bindings

| Key/Chord | Action |
| --------- | ------ |
| `C-x =` | Display a message in echo area, describing the character at point |
| `M-=` | Display the number of lines, words and characters present in the region |
| `C-u M-=` | Display the number of lines, words and characters for the entire buffer |
| `C-g` | Signal a ‘quit’ condition. Quits from any operation you want to get out of and cancel it. |
| `C-x C-c` | Kill emacs saving all currently opened files, this will kill the current connection terminating the emacs program |
| `C-z` | If using a text terminal, this suspends Emacs, on GUI this iconifies or minimizes the selected frame |


## Prefix Keys

A prefix key combines with the following input event to make a longer key sequence.  For example, ‘C-x’ is a prefix key, so typing ‘C-x’ alone does not invoke a command; instead, Emacs waits for further input (if you pause for longer than a second, it echoes the ‘C-x’ key to prompt for that input in the echo area.

| Key/Chord | Context |
| --------- | ------- |
| `C-c` | Access to mode specific commands |
| `C-h` | Access to help mode specific commands |
| `C-x` | General emacs editing and usage commands |
| `C-x <RET>` | Access to input method, coding system and language settings |
| `C-x @` | Add the event or key modifier keys to a key sequence |
| `C-x a` | Access to mode abreviations |
| `C-x n` | Access to narrowing (hiding text) mode commands |
| `C-x r` | Access to register commands. Registers are useful to temporarly save and retrieve content |
| `C-x t` | Access to tab creation and manipulation commands |
| `C-x v` | Access to version control commands |
| `C-x 4` | Access to window creation and manipulation commands |
| `C-x 5` | Access to frame creation and manipulation commands |
| `C-x 6` | Access to "two-column" editing commands |
| `M-g` | Access to goto jump and movement commands |


## Basic Editing Commands

These commands are the basics for entering text, make corrections and saving the work to files.


### Inserting Text

To insert text in emacs you simply type the associated key with the ordinary "graphic character". This adds the charager to the currently selected buffer at the current point. The insertion moves the cursor one point foward, the point remains just after the inserted text.

| Key/Chord | Action |
| --------- | ------ |
| `<RET>` | Ends a line by inserting `\n` and puts the cursor on a newly created line, indenting according with the current major mode |
| `C-j` | Inserts a new line as `<RET>` but does not do auto-indentation |
| `<insert>` | This is the insert key in your keyboard. Ths activates overwrite-mode that replaces the current characters at point as you type, eating the forward characters from the paragraph instead of pushing them to the right |
| `C-q` | Insert a character not supported by the keyboard, this quotes the charater you insert after the prefix. You can use octal or decimal and hexadecimal sequences to insert character codes if `read-quoted-char-radix` is set |
| `C-x 8 [shorthand]` | Insert unicode chars at point given the shorthands. See `C-x 8 C-h` for shorthands |
| `C-x 8 <RET>` | Prompts the user to insert the unicode name or code point of a character using the minibuffer |


### Changing The Location Of Point

| Key/Chord | Action |
| --------- | ------ |
| `C-f` | Moves foward one character in the buffer, does this by invoking '(forward-char &optional N)' command |
| `C-b` | Moves backward one character in the buffer, does this by invoking '(backward-char &optional N)' command |
| `C-a` | Moves to the beginning of the current line invoking '(move-beginning-of-line ARG)' elisp function |
| `C-e` | Moves to the end of the current line invoking '(move-end-of-line ARG)' elisp function |
| `C-n` | Moves vertically down one line or various if provided by invoking '(next-line &optional ARG TRY-VSCROLL)' function |
| `C-p` | Moves vertically up one line or various if provided by invoking '(previous-line &optional ARG TRY-VSCROLL)' function |
| `M-f` | Move foward one word in the current buffer |
| `M-b` | Move backwards one word in the current buffer |
| `M-e` | Move fowards to next end of sentence in the current buffer |
| `M-a` | Move backwards to the start of sentence in the current buffer |
| `C-x [` | Move backwards to page boundary. This accepts a numeric argument to repeat |
| `C-x ]` | Move forwards to page boundary. You can provide a numeric argument to repeat |
| `C-M-f` | Move forward across one balanced expression. You can provide positive and negative arguments to repeat |
| `C-M-b` | Move backward across one balanced expression (sexp). If positive numeric arg repeate many times, in reverse if negative argument |
| `C-M-e` | Move forward to the next end of a defun |
| `C-M-a` | Move backward to the beginning of a defun |
| `C-x <` | Scroll the selected window display N columns to the left, default window with minus 2 |
| `C-x >` | Scroll the selected window display N columns to the right |
| `M-r` | Reposition point on the left margin of the center-most, bottom-most and top-most line of the window. This cicles on consecutive invocations |
| `M-<` | Move to the top of the buffer |
| `M->` | Move to the end of the buffer |
| `C-v` | Scroll the display one screen foward and move point if necessary |
| `M-v` | Scroll one screen backward and move point if necessary |
| `M-g c` | Read a number N and move point to buffer position N. You can use `C-x =` to see the current point |
| `M-g g` | Read a number N and move point to the beginning of line number N. Line 1 is the beginning of the buffer |
| `M-g <TAB>` | Read a number N and move to coumn N in the current line. Column 0 is the leftmost column |
| `C-x C-n` | Use the current column point as the semipermantent goal column for `C-n` and `C-p` in the current buffer |
| `C-u C-x C-n` | Cancel the goal column, `C-n` and `C-p` preserve the horizontal position as usual |


### Erasing Text

| Key/Chord | Action |
| --------- | ------ |
| `<BACKSPACE>`, `<DEL>`, `<--` | Delete the character before point, or an active region |
| `<Delete>`, `<Supr>` | Delete the character after the point, or an active region |
| `C-d` | Delete the character after point, regardless of whether the region is active |
| `C-k` | Kill to the end of the line |
| `M-d` | Kill forward to the end of the next word |
| `M-<BACKSPACE>` | Kill back to the beginning of the previous word |


### Undoing Changes

| Key/Chord | Action |
| --------- | ------ |
| `C-/`, `C-x u`, `C-_` | Undo one entry of the undo records |


### Files

Text that you insert in an Emacs buffer lasts only as long as the Emacs session.  To keep any text permanently, you must put it in a “file”.

| Key/Chord | Action |
| --------- | ------ |
| `C-x C-f` | Prompt the user to type a file name in the minibuffer, it opens the file to visit if exist or creates a new buffer with that name if the file does not exist |
| `C-x C-r` | Prompt the user to type a file name in the minibuffer, it opens the file to visit without allowing to make changes to it |
| `C-x C-s` | Copies the altered buffer contents into the named file (if visiting) or prompts the user in the minibuffer to provide a name for a new file in disk |


### Blank Lines

Here are special commands and techniques for inserting and deleting
blank lines.

| Key/Chord | Action |
| --------- | ------ |
| `C-o` | Insert a blank line after the cursor (open-line) |
| `C-x C-o` | Delete all but one of many consecutive blank lines (delete-blank-lines) |


### Numeric Arguments

You can provide numeric arguments to commands for repetition count. The easiest way is to type a digit or a minus sign while holding down the <Meta> key. The minus sign is to provide a negative argument.

```
M-[arg] [command]
```

| Key/Chord | Action |
| --------- | ------ |
| `M-[arg] [command]` | Execute command successively [arg] times |
| `C-u [arg] [command]` | Execute command successively [arg] times or four times if [arg] is not provided. This also works as a "by four" multiplier |


### Repeating a Command

| Key/Chord | Action |
| --------- | ------ |
| `C-x z` | Repeats the previous emacs command many times if arg given or using the same arguments used before |
| `C-x z+` | Repeats the previous emacs command many times if arg given or using the same arguments used before. Each 'z' you type repeats the command one more time (notice the regex +) |